package com.cxs.musicplaywidget.service.idl;

import com.cxs.musicplaywidget.commonevent.NotificationManager;
import com.cxs.musicplaywidget.data.EventParam;
import com.cxs.musicplaywidget.commonevent.EventManager;
import com.cxs.musicplaywidget.player.IMusicPlayer;
import com.cxs.musicplaywidget.player.data.MusicData;
import com.cxs.musicplaywidget.player.player.IPlayerListener;
import com.cxs.musicplaywidget.player.player.MusicPlayer;
import com.cxs.musicplaywidget.util.LogUtils;
import ohos.app.Context;
import ohos.bluetooth.DataListenSocket;
import ohos.rpc.MessageOption;
import ohos.rpc.MessageParcel;
import ohos.rpc.RemoteException;
import ohos.utils.zson.ZSONObject;


import static com.cxs.musicplaywidget.constant.Const.IDL.*;

/**
 * @ClassName MyAgentStub
 * @author cxs
 * @version 1.0
 * @Description 远程代理(服务端) - 接收客户端发送过来的消息进行处理
 * @createTime 2022/3/7
*/
public class MyAgentStub extends AbsRemoteObject {
    private static final String tag = MyAgentStub.class.getSimpleName();
    private static IMusicPlayer musicPlayer;
    class PlayerListener implements IPlayerListener {
        @Override
        public void onPlayBackComplete() {
            LogUtils.debug(tag, "onPlayBackComplete()");
            EventParam eventParam = command(new EventParam(EventParam.EventAction.NEXT));
            LogUtils.debug(tag, "onPlayBackComplete.eventParam=" + eventParam);
            eventParam.setEventAction(EventParam.EventAction.GET_PLAY_LIST);
            EventManager.publishEvent(eventParam);
        }
    };

    public MyAgentStub(Context context) {
        super(context);
        musicPlayer = MusicPlayer.build(context);
        musicPlayer.setPlayerListener(new PlayerListener());
    }

    @Override
    public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) throws RemoteException {
        switch (code) {
            case REQUEST_CODE: {
                String s = data.readString();
                EventParam eventParam = ZSONObject.stringToClass(s, EventParam.class);

                // 执行播放指令并获得处理后的EventParam对象
                EventParam replyParam = command(eventParam);

                reply.writeInt(ERR_OK);
                String zsonString = ZSONObject.toZSONString(replyParam);
                reply.writeString(zsonString);

                if (eventParam.getEventAction() == EventParam.EventAction.GET_MUSIC_INFO) {
                    // 获取封面图片的数据流
                    byte[] thumbnail = replyParam.readThumbnail();
                    int length = (thumbnail == null) ? 0 : thumbnail.length;
                    reply.writeInt(length);
                    if (length > 0) {
                        reply.writeRawData(thumbnail, length);
                    }
                }
                // 发布事件更新卡片
                EventManager.publishEvent(replyParam);
                return true;
            }
            case REQUEST_GET_MUSIC_DATA_CODE: {
                // 获取歌曲文件数据流
                int musicIndex = data.readInt();
                MusicData musicData = musicPlayer.getMusicData(musicIndex);
                reply.writeInt(musicData.getLen());
                boolean result = reply.writeRawData(musicData.readData(), musicData.getLen());
                LogUtils.debug(tag, "onRemoteRequest.REQUEST_GMD_CODE.result=" + result);
                return true;
            }
            default:
                return false;
        }
        // EventParam的设备切换回本地(对发起连接的客户端来说是远程设备)
//        replyParam.setDeviceIndex(0);
    }


    /**
     * 执行播放指令
     * @param data
     * @return
     */
    public EventParam command(EventParam data) {
        EventParam eventParam = data.dup();
        LogUtils.debug(tag, "command.eventParam before = " + eventParam);
        switch (eventParam.getEventAction()) {
            case CONNECTED:
                eventParam.setPlayerState(getPlayerState());
                break;
            case GET_PLAYER_STATE: {
                eventParam.setPlayIndex(musicPlayer.getPlayIndex());
                eventParam.setPlayerState(getPlayerState());
                break;
            }
            case GET_PLAY_LIST: {
                LogUtils.debug(tag, "command.GET_PLAY_LIST.musicPlayer="+ musicPlayer);
                eventParam.setPlayerState(getPlayerState());
                eventParam.setPlayIndex(musicPlayer.getPlayIndex());
                eventParam.setPlayList(musicPlayer.getPlayList());
                LogUtils.debug(tag, "command.GET_PLAY_LIST.musicPlayer.getPlayList()="+ musicPlayer.getPlayList());
                break;
            }
            case RELOAD_PLAY_LIST: {
                eventParam.setPlayerState(getPlayerState());
                eventParam.setPlayIndex(musicPlayer.getPlayIndex());
                eventParam.setPlayList(musicPlayer.getPlayList(true));
                break;
            }
            case PLAY: {
                // 播放
                LogUtils.debug(tag, "command.PLAY.eventParam.getPlayIndex()=" + eventParam.getPlayIndex());
                if (eventParam.getPlayIndex() != -1) {
                    musicPlayer.play(eventParam.getPlayIndex());
                } else {
                    musicPlayer.play();
                }
                eventParam.setPlayIndex(musicPlayer.getPlayIndex());
                eventParam.setPlayerState(EventParam.PlayerState.PLAYING);
                LogUtils.debug(tag, "command.eventParam after assignThumbnail = " + eventParam);

                NotificationManager.getInstance(context).sendNotification(eventParam);
                LogUtils.debug(tag, "command.eventParam after sendNotification = " + eventParam);

                break;
            }
            case PAUSE: {
                // 暂停
                musicPlayer.pause();
                eventParam.setPlayerState(EventParam.PlayerState.PAUSED);

                NotificationManager.getInstance(context).sendNotification(eventParam);
                break;
            }
            case PREVIOUS: {
                // 上一首
                int previous = musicPlayer.previous();
                musicPlayer.play(previous);
                eventParam.setPlayIndex(previous);
                eventParam.setPlayerState(EventParam.PlayerState.PLAYING);
                eventParam.setPlayList(musicPlayer.getPlayList());

                NotificationManager.getInstance(context).sendNotification(eventParam);
                break;
            }
            case NEXT: {
                // 下一首
                int next = musicPlayer.next();
                LogUtils.debug(tag, "command.NEXT.next =" + next);
                musicPlayer.play(next);
                eventParam.setPlayIndex(next);
                eventParam.setPlayerState(EventParam.PlayerState.PLAYING);
                eventParam.setPlayList(musicPlayer.getPlayList());

                NotificationManager.getInstance(context).sendNotification(eventParam);
                break;
            }
            case RANDOM: {
                // 随机播放
                int random = musicPlayer.random();
                musicPlayer.play(random);
                eventParam.setPlayIndex(random);
                eventParam.setPlayerState(EventParam.PlayerState.PLAYING);
                eventParam.setPlayList(musicPlayer.getPlayList());

                NotificationManager.getInstance(context).sendNotification(eventParam);

                break;
            }
            case LOOP: {
                // 循环播放
                musicPlayer.play();
                eventParam.setMusicInfo(musicPlayer.getMusic());

                NotificationManager.getInstance(context).sendNotification(eventParam);

                break;
            }
            case GET_MUSIC_INFO: {
                eventParam.setMusicInfo(musicPlayer.getMusic());
                break;
            }
            case GET_THUMBNAIL: {
                eventParam.writeThumbnail(musicPlayer.getThumbnail());
                break;
            }
            case GET_MUSIC_DATA: {
//                MusicData data1 = eventParam.readMusicData();
//                MusicData data2 = musicPlayer.getMusicData(eventParam.getPlayIndex(), data1.getSkip());
//                eventParam.writeMusicData(data2);
                break;
            }
            case DECREASE: {
                musicPlayer.decrease();
                break;
            }
            case INCREASE: {
                musicPlayer.increase();
                break;
            }
            case GET_VOLUME: {
                eventParam.setMaxVolume(musicPlayer.getMaxVolume());
                eventParam.setVolume(musicPlayer.getVolume());
            }
            default: {
                LogUtils.debug(tag, "command.default");
                eventParam.setPlayerState(getPlayerState());
            }
        }
        LogUtils.debug(tag, "command.eventParam after = " + eventParam);
        return eventParam;
    }

    private EventParam.PlayerState getPlayerState() {
        if (musicPlayer.isPlaying()) {
            return EventParam.PlayerState.PLAYING;
        } else {
            return EventParam.PlayerState.PAUSED;
        }
    }

}
